﻿package com.eleme.dbutil;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/** 
 * ClassName: DbLink <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason: TODO ADD REASON(可选). <br/> 
 * date: 2017年12月23日 下午3:24:48 <br/> 
 * 
 * @author Pan 
 * @version  
 * @since JDK 1.8 
 */  
public class DbLink {


	private static Connection con;
	
	private static String DRIVER, URL, ACC, PW;
	
	
	private DbLink() {
		
	}
	
	static {
		readDriver();
		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(URL, ACC, PW);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() {
		return con;
	}
	
	private static void readDriver() {
		Properties p = new Properties();
		try {
			String Path = URLDecoder.decode(DbLink.class.getResource("/dbconf.properties").getPath(), "UTF-8");
			p.load(new FileInputStream(Path));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		DRIVER = p.getProperty("driver");
		URL = p.getProperty("url");
		ACC = p.getProperty("account");
		PW = p.getProperty("password");
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
